# Replication code for Taylor C. Boas and Amy Erica Smith, “Looks Like Me, Thinks Like Me: Descriptive Representation and Opinion Congruence in Brazil.” Latin American Research Review 54, 2 (2019).

# Analysis conducted in R 3.6.0 on MacOS 10.13.6

# NOTE: This file replicates Main Text Table 1 and Appendix Figure 8. We recommend running R replication files in the following order; please see readme.txt for details.
# 	1_merge_lapop.R
# 	2_merge_latinobarometro.R
# 	3_recode_reshape.R
# 	4_difference_in_distributions.R
# 	5_regressions.R
# 	6_civil_society_meeting.R
# 	7_mass_descriptives.R
# 	8_elite_descriptives.R
# 	9_mean_differences.R
#	10_difference_in_distributions_ks.R
#	11_elite_sample_simulation.R
#	12_converts_vs_lifelong.R

# Set working directory as appropriate
# setwd('~/Dropbox/brazil_leg_surveys/replication/')

# Clean desktop and load packages. Please make sure all necessary packages are installed.

rm(list=ls(all=T))

library(foreign)
library(car)
library(Hmisc)

scale01<-function(x) (x - min(x,na.rm=T))/diff(range(x,na.rm=T))

ecdf.diff<-function(x,y){
	vals<-sort(unique(c(x,y)))
	Fy<-ecdf(y)(vals)
	Fx<-ecdf(x)(vals)
	area<-0
	for(i in 1:(length(vals)-1)){
		area<-area+abs(diff(vals[i:(i+1)])*(Fx[i]-Fy[i]))
	}
	area
}

load('elite.data.RData')

# Load Pew Latin American religion survey files. 

pew06<-read.spss('pew forum 10-country religion survey data.sav',to.data.frame=T,use.value.labels=T)
pew06<-pew06[pew06$country=='Brazil',]
pew14<-read.spss('~/Dropbox/brazil_leg_surveys/Pew data/Religion-in-Latin-America-Dataset-and-Codebook/Religion in Latin America Dataset.sav',to.data.frame=T,use.value.labels=T)
pew14<-pew14[pew14$Country=='Brazil',]

# ====
# 2006
# ====

pew06$evang<-unclass(pew06$q3bra)==2
pew06$convert<-pew06$q8=='No'
pew06$econ<-scale01(ifelse(unclass(pew06$Q36a) %in% 5:6, NA, -1* unclass(pew06$Q36a)))
pew06$ideol<-scale01(ifelse(unclass(pew06$q41) > 10, NA, unclass(pew06$q41)))
pew06$abortion<-scale01(ifelse(unclass(pew06$Q36c) %in% 5:6, NA, unclass(pew06$Q36c)))

reg.econ.06<-with(pew06[pew06$evang,],lm(econ~convert))
reg.ideol.06<-with(pew06[pew06$evang,],lm(ideol~convert))
reg.abortion.06<-with(pew06[pew06$evang,],lm(abortion~convert))

diff.econ.life.evang.06<-ecdf.diff(elite.data$elite_econ[elite.data$elite_evang&elite.data$elite_year==2009],pew06$econ[pew06$evang&!pew06$convert])
diff.econ.convert.evang.06<-ecdf.diff(elite.data$elite_econ[elite.data$elite_evang&elite.data$elite_year==2009],pew06$econ[pew06$evang&pew06$convert])

diff.ideol.life.evang.06<-ecdf.diff(elite.data$elite_ideol[elite.data$elite_evang&elite.data$elite_year==2009],pew06$ideol[pew06$evang&!pew06$convert])
diff.ideol.convert.evang.06<-ecdf.diff(elite.data$elite_ideol[elite.data$elite_evang&elite.data$elite_year==2009],pew06$ideol[pew06$evang&pew06$convert])

# ====
# 2014
# ====

pew14$evang<-unclass(pew14$QCURRELrec)==2
pew14$evang.raised<-ifelse(unclass(pew14$QCHREL) %in% 5:6, NA, unclass(pew14$QCHREL)==2)
pew14$evang.convert<-pew14$evang & ! pew14$evang.raised

pew14$econ<-scale01(ifelse(unclass(pew14$Q19b) %in% 5:6, NA, -1* unclass(pew14$Q19b)))
pew14$ideol<-scale01(ifelse(unclass(pew14$Q81BRA) > 7, NA, unclass(pew14$Q81BRA)))
pew14$gaymarriage<-scale01(ifelse(unclass(pew14$Q15) %in% 5:6, NA, unclass(pew14$Q15)))
pew14$abortion<-scale01(ifelse(unclass(pew14$Q16) %in% 5:6, NA, unclass(pew14$Q16)))

reg.econ.14<-with(pew14[pew14$evang,],lm(econ~evang.convert))
reg.ideol.14<-with(pew14[pew14$evang,],lm(ideol~evang.convert))
reg.gaymarriage.14<-with(pew14[pew14$evang,],lm(gaymarriage~evang.convert))
reg.abortion.14<-with(pew14[pew14$evang,],lm(abortion~evang.convert))

diff.econ.life.evang<-ecdf.diff(elite.data$elite_econ[elite.data$elite_evang&elite.data$elite_year==2013],pew14$econ[pew14$evang&!pew14$evang.convert])
diff.econ.convert.evang<-ecdf.diff(elite.data$elite_econ[elite.data$elite_evang&elite.data$elite_year==2013],pew14$econ[pew14$evang&pew14$evang.convert])

diff.ideol.life.evang<-ecdf.diff(elite.data$elite_ideol[elite.data$elite_evang&elite.data$elite_year==2013],pew14$ideol[pew14$evang&!pew14$evang.convert])
diff.ideol.convert.evang<-ecdf.diff(elite.data$elite_ideol[elite.data$elite_evang&elite.data$elite_year==2013],pew14$ideol[pew14$evang&pew14$evang.convert])

diff.gaymarriage.life.evang<-ecdf.diff(elite.data$elite_gaymarriage[elite.data$elite_evang],pew14$gaymarriage[pew14$evang&!pew14$evang.convert])
diff.gaymarriage.convert.evang<-ecdf.diff(elite.data$elite_gaymarriage[elite.data$elite_evang],pew14$gaymarriage[pew14$evang&pew14$evang.convert])

diff.abortion.life.evang<-ecdf.diff(elite.data$elite_abortion[elite.data$elite_evang],pew14$abortion[pew14$evang&!pew14$evang.convert])
diff.abortion.convert.evang<-ecdf.diff(elite.data$elite_abortion[elite.data$elite_evang],pew14$abortion[pew14$evang&pew14$evang.convert])

# =======================================================================
# Main Text Table 1: Issue Opinion: Lifelong Evangelicals versus Converts
# =======================================================================

meandiff.table<-round(matrix(c(
	coef(reg.econ.06)[1],sum(coef(reg.econ.06)), coef(summary(reg.econ.06))[2,4],
	coef(reg.econ.14)[1],sum(coef(reg.econ.14)), coef(summary(reg.econ.14))[2,4],
	coef(reg.ideol.06)[1],sum(coef(reg.ideol.06)), coef(summary(reg.ideol.06))[2,4],
	coef(reg.ideol.14)[1],sum(coef(reg.ideol.14)), coef(summary(reg.ideol.14))[2,4],
	coef(reg.abortion.06)[1],sum(coef(reg.abortion.06)), coef(summary(reg.abortion.06))[2,4],
	coef(reg.abortion.14)[1],sum(coef(reg.abortion.14)), coef(summary(reg.abortion.14))[2,4],
	NA,NA,NA,
	coef(reg.gaymarriage.14)[1],sum(coef(reg.gaymarriage.14)), coef(summary(reg.gaymarriage.14))[2,4]),ncol=6,byrow=T),2)
rownames(meandiff.table)<-c('Economic Regime','Ideology','Abortion','Gay Marriage')
colnames(meandiff.table)<-c('Lifelong','Convert','P-value','Lifelong','Convert','P-value')
	
bottom.note<-"NOTE: All variables scaled 0 (left) to 1 (right). P-values are from two-sided difference-in-means t-tests assuming equal variances. Data are from surveys by the Pew Forum on Religion \\& Public Life: ``Spirit and Power: A 10-Country Survey of Pentecostals'' (2006) and ``Religion in Latin America'' (2014)." 

meandiff.table.latex<-latex(meandiff.table,file='evang_convert_vs_lifelong_table.tex',collabel.just=rep('c',6),col.just=rep('c',6), rowlabel = '', caption = 'Issue Opinion: Lifelong Evangelicals versus Converts', cgroup = c(2006,2014), n.cgroup = c(3,3), booktabs = F, ctable = T, where = "htp",insert.bottom=bottom.note,na.blank=T)

# ============================================================================
# Appendix Figure 8: Opinion Congruence: Lifelong Evangelicals versus Converts
# ============================================================================

congruence.data<-rbind(c(diff.econ.life.evang.06, diff.ideol.life.evang.06, diff.econ.life.evang, diff.ideol.life.evang, diff.gaymarriage.life.evang, diff.abortion.life.evang),c(diff.econ.convert.evang.06, diff.ideol.convert.evang.06, diff.econ.convert.evang, diff.ideol.convert.evang, diff.gaymarriage.convert.evang, diff.abortion.convert.evang))

pdf(file='evang_convert_vs_lifelong_plot.pdf',height=6,width=7.5)
par(mar=c(5,6,2,2)+0.1)
congruence.plot<-barplot(congruence.data,beside=T,ylim=c(0,0.3),ylab=expression(integral(abs(F[e](x)-F[m](x))*dx,0,1)),legend.text=c('Lifelong Evangelicals','Evangelical Converts'))
axis(side=1,at=apply(congruence.plot,2,mean),tick=F,labels=c('Economic\nRegime\n 2006','Ideology\n2006','Economic\nRegime\n 2014','Ideology\n2014','Abortion\n2014','Gay\nMarriage\n2014'),line=1.5)
dev.off()